/**
 * @author Manoj
 * CsscommandGenerator.java
 * This class handles Css tags.
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guiregistration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

import utility.Input;

public class CsscommandGenerator
{		
	RandomAccessFile cssfile;
	String cssClasses; // full CSS object DATA.
	int sequenceId; // Sequence ID of The Style Object and from its Master. 
	String Tag;

	/**
	 * called after getting Css in the html file and with All CSS classes.
	 * @param cssdata
	 * @param style_sequenceId 
	 */
	CsscommandGenerator(String cssdata,String style_sequenceId)
	{
		this.cssClasses=cssdata;
		String[] style_seqId=style_sequenceId.split("_");
		this.sequenceId=Integer.valueOf(style_seqId[1]);
		this.Tag=style_sequenceId;
		
		try 
		{
			this.cssfile= new RandomAccessFile(new File(Input.COMMANDOUTPUT+"CSS"+".txt"),"rw");
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}	
	}
		
	/**
	 * generates The CSS Commands.
	 *
	 */
	public void	generateCssCommands()   
	{
		try 
		{
			ArrayList<Object> Cclsses= new ArrayList<Object>();
			String[] Classes=cssClasses.split("}");
			
			for (int i = 0; i < Classes.length; i++)
			{
				String tempCSSobject=Classes[i]; //Complete CSS CLASS WITH PROPERTIES
				
				if(tempCSSobject.length()>4)
				{
					StringBuffer name_sbr=new StringBuffer("\"CSS_"+sequenceId+"_"+i+"\"");
					StringBuffer Command_sbr= new StringBuffer("nm*\"CSS_"+sequenceId+"_"+i+"\"");
					
					//String tempCSSObjectname= new String(Command_sbr.toString());
					
					Cclsses.add(name_sbr.toString());
					//CSS NAME.
					int indexofOpen=tempCSSobject.indexOf("{");
					
					String CSSNAME=tempCSSobject.substring(0,indexofOpen).replace("\r\n", " ");
					CSSNAME=CSSNAME.replace('\t', ' ');
					CSSNAME=CSSNAME.trim();
					Command_sbr.append("|CssName:\""+CSSNAME+"\"!0|");
					//CSS properties
					String CSSPpt=tempCSSobject.substring(indexofOpen+1).replace('\n', ' ');
					CSSPpt=CSSPpt.trim();
					
					String[]CSSproperTies=CSSPpt.split(";");
					System.out.println("CSS OBJECT"+Command_sbr);
					int ref=1;
					for (int j = 0; j < CSSproperTies.length; j++) 
					{
						String EachProperty=CSSproperTies[j];
						// removing all nextline Tabs amd Caariage return.
						
						if(EachProperty.contains("\n"))
							EachProperty=EachProperty.replace('\n', ' ');
						
						if(EachProperty.contains("\r"))
							EachProperty=EachProperty.replace('\r', ' ');
						
						if(EachProperty.contains("\t"))
							EachProperty=EachProperty.replace('\t', ' ');
						
						EachProperty=EachProperty.trim();
						
						int Colanindex=EachProperty.indexOf(":");
						String Name=EachProperty.substring(0,Colanindex);
						
						String Value=EachProperty.substring(Colanindex+1,EachProperty.length());
						
						Command_sbr.append("\""+Name.trim()+"\":\""+Value.trim()+";\"!"+ref+"|");
						ref++;
					}
					Command_sbr.deleteCharAt(Command_sbr.length()-1);
					Command_sbr.append("#605\n");
					byte[] OutCommand= Command_sbr.toString().getBytes();
				
					cssfile.write(OutCommand);
				}
				
			}
			StyleMasterCommands(Cclsses);
			cssfile.close();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
		
	
	/**
	 * StyleMasterCommands  is for The  generating Single Style object Extara properties with The found out css objects.
	 * @param cssObject
	 */
	private	void  StyleMasterCommands(ArrayList<Object> cssObject)
	{
		StringBuffer m_command= new StringBuffer("nm*\""+Tag+"\"|");
		
		for (int i = 0; i < cssObject.size(); i++) 
		{
			String className=(String)cssObject.get(i);
			m_command.append("CSS:"+className+"!"+(i+1)+"|");
		}
		
		m_command.deleteCharAt(m_command.length()-1);
		m_command.append("#605\n");
		
		try 
		{
			cssfile.write(m_command.toString().getBytes());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}		
	}
}

